Telegram Group & Telegram Channel
🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections



tg-me.com/pro_python_code/1841
Create:
Last Update:

🐍 Задача на Python — Топ‑3 самых популярных товаров

Допустим, у нас есть список покупок пользователей:


orders = [
{"user": "alice", "items": ["apple", "banana", "apple"]},
{"user": "bob", "items": ["banana", "orange"]},
{"user": "carol", "items": ["banana", "apple", "orange", "banana"]},
{"user": "dave", "items": ["apple"]},
]


🎯 Задание:
Найти 3 самых популярных товара по количеству покупок (не по числу пользователей, а по общему количеству упоминаний).

Ожидаемый результат:

[('banana', 4), ('apple', 4), ('orange', 2)]

💡 Решение:




from collections import Counter

# Собираем все товары в один список
all_items = []
for order in orders:
all_items.extend(order["items"])

# Считаем количество каждого товара
item_counts = Counter(all_items)

# Получаем топ-3 самых популярных
top_3 = item_counts.most_common(3)

print(top_3)


📌 Что тренирует задача:

• Работа со словарями и списками
• Использование Counter из модуля collections
• Умение работать с вложенными структурами
• Сортировка по частоте с помощью most_common()

#python #задача #кодинг #алгоритмы #учимпитон #collections

BY Python RU


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/pro_python_code/1841

View MORE
Open in Telegram


Python RU Telegram | DID YOU KNOW?

Date: |

For some time, Mr. Durov and a few dozen staffers had no fixed headquarters, but rather traveled the world, setting up shop in one city after another, he told the Journal in 2016. The company now has its operational base in Dubai, though it says it doesn’t keep servers there.Mr. Durov maintains a yearslong friendship from his VK days with actor and tech investor Jared Leto, with whom he shares an ascetic lifestyle that eschews meat and alcohol.

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

Python RU from hk


Telegram Python RU
FROM USA